﻿
using System;
using System.Collections.Generic;
using System.Data.Entity;
using HQPad.Models;
using HQPad.DAL;
using System.Data.Entity.ModelConfiguration.Conventions;

namespace HQPad.Models
{
    public class HQContext : DbContext, IUnitOfWork
    {
        public DbSet<Guild> Guilds { get; set; }
        public DbSet<PointsTable> PointsTables { get; set; }
        public DbSet<SkillCost> SkillCosts { get; set; }
        public DbSet<SkillRequirement> SkillRequirements { get; set; }
        public DbSet<Skill> Skills { get; set; }
        public DbSet<RuleSet> RuleSets { get; set; }
        public DbSet<Bonus> Bonuses { get; set; }
        public DbSet<Spell> Spells { get; set; }
        public DbSet<Invocation> Invocations { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

            modelBuilder.Entity<SkillRequirement>()
                .HasRequired(p => p.ParentSkill)
                .WithMany(d => d.Requirements)
                .HasForeignKey(p => p.ParentSkillID)
                .WillCascadeOnDelete(true);

            modelBuilder.Entity<Guild>()
                .HasMany(g => g.AllowedGuilds)
                .WithMany(g => g.PrimaryParents)
                .Map(m => 
                {
                    m.ToTable("SecondaryGuilds");
                    m.MapLeftKey("PrimaryGuildID");
                    m.MapRightKey("AllowedGuildID");
                });
        }

        public void Save()
        {
            SaveChanges();
        }

        public void Dispose()
        {
            base.Dispose();
        }
    }
}